<?php
/**
 * RbacRoles
 * @文件名称: RbacRolesModel.php
 * @author: jawei
 * @Email: gaozhiwei@etcp.cn
 * @Date: 2017-06-06
 * @Copyright: 2017 悦畅科技有限公司. All rights reserved.
 * 注意：本内容仅限于悦畅科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
namespace app\models;

use Yii;
use app\components\DmpUtil;
use app\base\BaseException;
use app\base\BaseModel;

class RbacRolesModel extends BaseModel
{
    public static function tableName()
    {
        return 'dmp_rbac_roles';
    }

    public static function getBatch($page, $roleName = null)
    {
        if (!DmpUtil::issetEmptyCheckBool($roleName)) {
            $rows = self::find()
                ->offset($page * Yii::$app->params['pagination'])
                ->limit(Yii::$app->params['pagination'])
                ->asArray()
                ->all();
        } else {
            $rows = self::find()
                ->where('name like :n', [':n' => "%$roleName%"])
                ->offset($page * Yii::$app->params['pagination'])
                ->limit(Yii::$app->params['pagination'])
                ->asArray()
                ->all();
        }

        return $rows;
    }

    public static function getAll()
    {
        $rows = self::find()
            ->asArray()
            ->all();

        return $rows;
    }

    public static function getById($id)
    {
        if (!DmpUtil::issetEmptyCheckBool($id)) {
            throw new BaseException('empty user id array in rbac role model', 40353);
        }

        $rows = self::find()
            ->where('id = :id', [':id' => $id])
            ->asArray()
            ->one();
        return $rows;
    }

    public static function getByIds(array $ids)
    {
        if (!DmpUtil::issetEmptyCheckBool($ids)) {
            throw new BaseException('empty user id array in rbac role model', 40353);
        }

        if (is_array($ids)) {
            $idString = implode(',', $ids);
            $where = "id in ({$idString})";
        } else {
            $where = "id = $ids";
        }
        $rows = self::find()
            ->where($where)
            ->asArray()
            ->all();
        return $rows;
    }

    public static function getAmount($roleName)
    {
        if (!DmpUtil::issetEmptyCheckBool($roleName)) {
            $amount = self::find()
                ->count('*');
        } else {
            $amount = self::find()
                ->where('name like :n', [':n' => "%$roleName%"])
                ->count('*');
        }

        return $amount;
    }

    public static function isExists($name)
    {
        $result = self::find()
            ->where('name = :n', [':n' => $name])
            ->exists();
        return $result;
    }

    public static function addOne($data)
    {
        $model = new RbacRolesModel();
        $model->name = $data['name'];
        $model->perm_ids = $data['perm_ids'];
        $model->routes = $data['routes'];
        $model->description = '';
        $model->time_create = time();
        return $model->save();
    }

    public static function updateOne($id, $data)
    {
        $model = self::findOne($id);
        $model->name = $data['name'];
        $model->perm_ids = $data['perm_ids'];
        $model->routes = $data['routes'];
        return $model->save();
    }

    public static function deleteOne($id)
    {
        $model = self::findOne($id);
        return $model->delete();
    }
}
